
/**
 * 行政老师查看某一入所年度的学生列表
 * 
 * /xingzheng/reaserchstudentlist
 * 
 */



const User_Info = require('../../../models/usersInfo')
const Research_Student = require('../../../models/researchTopicStudents')
const utils = require('../../../utils/index')
const $ = require('../../../utils/common')
const Sequelize = require('sequelize')
const Op = Sequelize.Op

const get_list = async function (req, res) {
 
    try {
        
        Research_Student.belongsTo(User_Info,
            {
                foreignKey: 'student_user_name', 
                targetKey: 'user_name'
            }
        )
    
        if (!req.query.year_id) {
            $.sendMessage(res, 1015, '参数 year_id 不存在')
            return
        }

        var query = {}
        
        if (req.query.teacher_fullname) { query['teacher_fullname'] = req.query.teacher_fullname}
        if (req.query.topic_name) {query['topic_name'] = req.query.topic_name}
        if (req.query.user_fullname) {query['user_fullname'] = req.query.user_fullname}
        if (req.query.user_name) {query['user_name'] = req.query.user_name}
        if (req.query.student_status) {query['student_status'] = req.query.student_status}


        var offset = 1
        var limit = parseInt(req.query.size)
        if (req.query.page > 1) {
            offset = parseInt(req.query.size) * (parseInt(req.query.page) - 1)
        } else if (req.query.page == 1) {
            offset = 0
        }

        // findAndCountAll这个API既查找还统计满足条件的记录数
        var result =  await Research_Student.findAndCountAll({
            offset: offset,
            limit: limit,
            attributes: ['id' ,'teacher_fullname', 'topic_name', 'student_status', 'topic_id'], 
            where: {
                student_year: req.query.year_id,
                teacher_fullname: {
                    [Op.like]: '%' + (query.teacher_fullname == undefined ? '' : query.teacher_fullname) + '%'
                },
                topic_name: {
                    [Op.like]: '%' + (query.topic_name == undefined ? '' : query.topic_name) + '%'
                },
                student_status: {
                    [Op.like]: '%' + (query.student_status == undefined ? '' : query.student_status) + '%'
                }
            },
            include: [
                { // include关键字表示关联查询
                    model: User_Info, // 指定关联的model
                    // as:'info', // 由于前面建立映射关系时为class表起了别名，那么这里也要与前面保持一致，否则会报错
                    attributes: ['user_fullname', 'user_name'], // 这里的attributes属性表示查询class表的name和rank字段，其中对name字段起了别名className
                    where: {
                        user_fullname: {
                            [Op.like]: '%' + (query.user_fullname == undefined ? '' : query.user_fullname) + '%'
                        },
                        user_name: {
                            [Op.like]: '%' + (query.user_name == undefined ? '' : query.user_name) + '%'
                        },
                    },
                }
            ],
            raw:true // 这个属性表示开启原生查询，原生查询支持的功能更多，自定义更强
        })

        res.json({
            code: 200,
            data: result
        })

        utils.create_a_log(req, '年度学生', '浏览入所学生')
    }
    catch(error) {
        res.json({
            code: 0000,
            message: '抓到异常',
            error: 'error' + error
        })
        utils.create_a_log(req, '年度学生', '浏览学生', '失败')
    }
}
    
module.exports = get_list